package 力扣日常刷题.木22二月.第13天219;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/2/19
 * @description
 * 快慢指针
 */
public class Demo103删除倒数第K个节点 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0,head);
        ListNode first = head;
        ListNode second = dummy;
        for (int i = 0; i < n; i++) first = first.next;

        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        ListNode ans = dummy.next;
        return ans;

    }

}
